package opencv;
import java.awt.Color;

import java.awt.image.BufferedImage;

import java.io.File;import java.io.IOException;

import javax.imageio.ImageIO;



public class GrayScale 

{	

	public static void main() throws IOException 	

	{		

		// Save the image file as a BufferedImage object		

		BufferedImage cat = ImageIO.read(new File("dest.jpg"));				

		

		// Loop through all the pixels in the image (w = width, h = height)		

		

		for(int w = 0; w < cat.getWidth() ; w++)		

		{			

			for(int h = 0 ; h < cat.getHeight() ; h++)			

			{				

				// BufferedImage.getRGB() saves the colour of the pixel as a single integer.				

				// use Color(int) to grab the RGB values individually.				

				

				Color color = new Color(cat.getRGB(w, h));								

				

				// use the RGB values to get their average.				

				int averageColor = ((color.getRed() + color.getGreen() + color.getBlue()) / 3);				

				

				// create a new Color object using the average colour as the red, green and blue				

				// colour values				

				Color avg = new Color(averageColor, averageColor, averageColor);								

				

				// set the pixel at that position to the new Color object using Color.getRGB().				

				cat.setRGB(w, h, avg.getRGB());			

			}		

		}				

		

		// save the newly created image in a new file.		

		ImageIO.write(cat, "jpg", new File("imgg11.jpg"));



	}

}
